package nongchang.kuaijiecaifu.com.ncapp_android.retrofit.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nongchang.kuaijiecaifu.com.ncapp_android.common.adapter.ItemAdapter;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.event.CartCountChange;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.AllCart;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.Cart;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.HomeItemCart;
import nongchang.kuaijiecaifu.com.ncapp_android.util.RxBus;

/**
 * Created by Administrator on 2016/6/12 0012.
 */
public class HomeCartManager {
    public static List<AllCart> cartList = new ArrayList<>();
    public static List<HomeItemCart> itemcartList;
    public static RemoteCart remote = new RemoteCart();

    //    本地购物车
    public static LocalCart local = new LocalCart();


    //    购物车数量
    public static int cartCount = 0;

    public static int count() {
        return cartCount;
    }

    public static void sync() {
        Collection<AllCart> items = HomeCartManager.local.items();
        cartList.clear();
        if (items.size() < 1) {
            return;
        }
        for (AllCart item : items) {
            cartList.add(item);
        }
    }

    public static class RemoteCart {

    }

    //    得到当前购物车的总金额
    public static float getTotal() {
        int sum = 0;
        for (int i = 0, size = cartList.size(); i < size; i++) {
            sum += cartList.get(i).money;
        }
        return sum;
    }

    //  得到当前adapter的所有购车数量
    public static int calcCoin(ItemAdapter<AllCart, ?> adapter) {
        int count = adapter.getItemCount();
        int coins = 0;
        for (int i = 0; i < count; i++) {
            coins += adapter.getItem(i).number;
        }
        return coins;
    }

    public static class LocalCart {
        public Map<String, AllCart> map = new HashMap<>();

        public Collection<AllCart> items() {
            return map.values();
        }

        //        移除商品
        public void remove(List<String> ids) {
            for (String id : ids) {
                map.remove(id);
            }
        }

        public void remove(String id) {
            map.remove(id);
            sync();
            RxBus.post(new CartCountChange(map.size()));
        }

        //        添加多个商品
        public void add(List<AllCart> carts) {
            for (int i = 0; i < carts.size(); i++) {
                AllCart cart = carts.get(i);
                map.put(cart.unitId, cart);
            }
            sync();
            RxBus.post(new CartCountChange(map.size()));
        }

        //        添加单个商品
        public void add(AllCart cart) {
            int size = map.size();
            map.put(cart.unitId, cart);

            if (size >= map.size()) {
                sync();
            } else {
                cartList.add(cart);
            }
            RxBus.post(new CartCountChange(map.size()));
        }
    }

}
